﻿using AutoMapper;
using Enter.Admin.Dtoes;
using Enter.Admin.Entities;
using Enter.Admin.Filters;
using Enter.Admin.Models;
using Enter.Admin.Models.DataTables;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Enter.Admin.Controllers
{
    [TypeFilter(typeof(ActionFilter))]
    public class NoticeController : BaseController
    {
        public NoticeController(ILogger<NoticeController> logger, IFreeSql fsql, IMapper mapper) : base(logger, fsql, mapper)
        {
        }
        // GET: NoticeController
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        // [ValidateAntiForgeryToken]
        public IActionResult Index([FromBody] DataTablesParameters<object> model)
        {
            var session = Session();
            var data = _fsql.Select<Notice>().Where(t => t.ToUserId == session.Id).OrderBy(t=>t.Readed).Count(out var total).Skip(model.Start).Take(model.Length).ToList();
            return Json(new DataTablesResult<NoticeModel>(model.Draw, total, total,_mapper.Map<List<NoticeModel>>(data)));
        }

        // GET: NoticeController/Detail/5
        [HttpPost]
        public ActionResult Detail(int id)
        {
            if (id == 0) {
                return Json(new ResponseResult<string>(false, "消息不存在."));
            }
            var session = Session();
            var model = _fsql.Select<Notice>(new { Id = id }).ToOne();
            var html =string.Format( @"
<h6 class=""media-heading"">
    {0}
</h6>
<div>{1}</div>
<div class=""text-muted f-s-12"">{2}</div>
", model.Title, model.Note, model.CreateTime.ToString("yyyy-MM-dd"));
            _fsql.Update<Notice>().Set(t => t.Readed, true).Where(t => t.Id == id).ExecuteAffrows();
            var count = _fsql.Select<Notice>().Where(t => t.ToUserId == session.Id && t.Readed == false).Count();
            return Json(new ResponseResult<object>(true,new {Html = html, Count = count }, "ok."));
        }

        // GET: NoticeController/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: NoticeController/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: NoticeController/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: NoticeController/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: NoticeController/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: NoticeController/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
    }
}
